package com.honglv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.honglv.context.BaseContext;
import com.honglv.mapper.FoodMapper;
import com.honglv.pojo.dto.PageByCityDTO;
import com.honglv.pojo.dto.UploadFoodDTO;
import com.honglv.pojo.entity.Food;
import com.honglv.pojo.vo.GetFoodsByCityVO;
import com.honglv.pojo.dto.PageDTO;
import com.honglv.pojo.dto.UpdateFoodDTO;
import com.honglv.pojo.result.Result;
import com.honglv.pojo.vo.GetAllFoodsVO;
import com.honglv.pojo.vo.GetFoodVO;
import com.honglv.pojo.vo.PageVO;
import com.honglv.service.FoodService;
import com.honglv.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class FoodServiceImpl implements FoodService {

    @Autowired
    private FoodMapper foodMapper;

    @Autowired
    private AliOssUtil aliOssUtil;
    /**
     * 管理员上传美食
     * @param
     * @return
     */
    @Override
    public void upload(UploadFoodDTO uploadFoodDTO) {
        Long authorId = BaseContext.getCurrentId();
        Food food = new Food();
        BeanUtils.copyProperties(uploadFoodDTO, food);
        food.setAuthorId(authorId);
        foodMapper.insert(food);
    }
    /**
     * 根据城市分页获取美食
     * @param pageByCityDTO
     * @return
     */

    @Override
    public PageVO<GetFoodsByCityVO> getFoodsByCity(PageByCityDTO pageByCityDTO) {
        PageHelper.startPage(pageByCityDTO.getPage(),pageByCityDTO.getPageSize());
        QueryWrapper<Food> qw=new QueryWrapper<>();
        qw.like("city",pageByCityDTO.getCity());
        List<Food> list = foodMapper.selectList(qw);
        GetFoodsByCityVO gf=new GetFoodsByCityVO();
        List<GetFoodsByCityVO> foods=new ArrayList<>();
        for(Food food:list){
            BeanUtils.copyProperties(food,gf);
            foods.add(gf);
        }
        if (foods == null){
            return null;
        }
        PageInfo<GetFoodsByCityVO> pageInfo = new PageInfo<>(foods);
        return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
    }




    @Override
    public Result deleteFood(Long id) {
        int count = foodMapper.deleteFood(id);
        return count > 0? Result.success("删除成功"):Result.success("无此数据");
    }

    @Override
    public Result updateFood(UpdateFoodDTO updateFoodDTO) {
        if(foodMapper.updateFood(updateFoodDTO) > 0){
            return Result.success("修改成功！");
        }
        return Result.error("修改失败，未知错误！");
    }
    /**
     * Description:获取美食详情页
     * date: 2024/04/01
     * author:王铭杰
     */
    public GetFoodVO getFood(Long id) {
        GetFoodVO getFoodVO = foodMapper.getFood(id);
        return getFoodVO;
    }

    /**
     * Description:分页获取美食
     * date: 2024/04/01
     * author:王铭杰
     */
    @Override
    public PageVO<GetAllFoodsVO> getAllFoods(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPage(),pageDTO.getPageSize());
        List<GetAllFoodsVO> list = foodMapper.getAllFoods();
        if (list == null){
            return null;
        }
        PageInfo<GetAllFoodsVO> pageInfo = new PageInfo<>(list);
        return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * Description:通过用户id获取美食
     * date: 2024/04/01
     * author:王铭杰
     */
    @Override
    public List<GetAllFoodsVO> getFoodsByAuthorId(Long userId) {
        List<GetAllFoodsVO> list = foodMapper.getFoodsByAuthorId(userId);
        return list;
    }
}


